<template>
  <div v-show="externalLoginFlag" class="login">
    <h3 class="login_title">山洪灾害监测预警 【四预】 系统</h3>
    <div class="login-form-wrap">
      <div class="login-form-content">
        <img class="login-title-img" src="~@/assets/img/welcome.png" alt="">
        <el-form ref="loginForm" :model="loginForm" :rules="loginRules" label-position="top" label-width="0px"
          class="login-form">
          <el-form-item prop="username">
            <el-input v-model="loginForm.username" type="text" auto-complete="off" placeholder="请输入用户名">
              <template #prepend>
                <div class="input-prefix-warp">
                  <img class="input-prefix-img" src="~@/assets/img/inputbg.png" alt="left-icon">
                  <img class="input-prefix-img2" src="~@/assets/img/yonghu-icon.png" alt="right-icon">
                </div>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input v-model="loginForm.password" type="password" auto-complete="off" placeholder="请输入密码"
              @keyup.enter.native="handleLogin">
              <template #prepend>
                <div class="input-prefix-warp">
                  <img class="input-prefix-img" src="~@/assets/img/inputbg.png" alt="left-icon">
                  <img class="input-prefix-img2" src="~@/assets/img/mima-icon.png" alt="right-icon">
                </div>
              </template>
            </el-input>
          </el-form-item>

          <el-form-item style="width:100%;">
            <el-button :loading="loading" size="medium" type="primary" class="login-btn"
              @click.native.prevent="handleLogin">
              <span v-if="!loading">登 录</span>
              <span v-else>登 录 中...</span>
            </el-button>
          </el-form-item>
        </el-form>
      </div>

    </div>

  </div>
</template>

<script>
import { encrypt } from '@/utils/rsaEncrypt'
import Config from '@/settings'
import { getCodeImg } from '@/api/login'
import Cookies from 'js-cookie'
export default {
  name: 'Login',
  data() {
    return {
      codeUrl: '',
      cookiePass: '',
      loginForm: {
        username: '',
        password: '',
        // username: 'csiit',
        // password: 'Csiit@2021',
        rememberMe: false,
        code: '',
        uuid: ''
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', message: '用户名不能为空' }],
        password: [{ required: true, trigger: 'blur', message: '密码不能为空' }],
        code: [{ required: true, trigger: 'change', message: '验证码不能为空' }]
      },
      loading: false,
      redirect: undefined,
      externalLoginFlag: true
    }
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  created() {
    const username = this.$route.query.username
    const password = this.$route.query.password
    if (username && password) {
      this.externalLoginFlag = false
      this.externalLogin({
        username: username,
        password: password
      })
    }
    // this.getCode()
    // this.getCookie()
  },
  mounted() {
    // this.$loadSvga('/svga/login.svga', '#login-left')
    // if (this.externalLoginFlag) { return }
  },
  methods: {
    getCode() {
      getCodeImg(this.loginForm.username, this.loginForm.password).then(res => {
        this.codeUrl = res.img
        this.loginForm.uuid = res.uuid
        console.log(res, 'this.codeUrl ')
      })
    },
    getCookie() {
      const username = Cookies.get('username')
      let password = Cookies.get('password')
      const rememberMe = Cookies.get('rememberMe')
      // 保存cookie里面的加密后的密码
      this.cookiePass = password === undefined ? '' : password
      password = password === undefined ? this.loginForm.password : password
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password,
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe),
        code: ''
      }
    },
    handleLogin() {
      const that = this
      // this.$refs.loginForm.validate(valid => {
      //     const user = {
      //         username: that.loginForm.username,
      //         password: that.loginForm.password,
      //         rememberMe: that.loginForm.rememberMe,
      //         code: that.loginForm.code,
      //         uuid: that.loginForm.uuid
      //     }
      //     if (user.password !== that.cookiePass) {
      //         user.password = encrypt(user.password)
      //     }
      //     // alert(JSON.stringify(user))
      //     // debugger
      //     if (valid) {
      //         that.loading = true
      //         if (user.rememberMe) {
      //             Cookies.set('username', user.username, { expires: Config.passCookieExpires })
      //             Cookies.set('password', user.password, { expires: Config.passCookieExpires })
      //             Cookies.set('rememberMe', user.rememberMe, { expires: Config.passCookieExpires })
      //         } else {
      //             Cookies.remove('username')
      //             Cookies.remove('password')
      //             Cookies.remove('rememberMe')
      //         }

      //         that.$store.dispatch('Login', user).then(() => {
      //             that.$router.push({ path: this.redirect || '/login' }, function() {
      //                 that.loading = false
      //             })
      //         }).catch(() => {
      //             that.loading = false
      //             that.getCode()
      //         })
      //     } else {
      //         console.log('error submit!!')
      //         return false
      //     }
      // })
      // if (this.loginForm.username === 'admin' && this.loginForm.password === 'csiit') {
      //     this.$router.push({ path: this.redirect || '/safeGeneral' })
      // } else {
      //     return this.$message.error('用户名或密码错误')
      // }
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.$store.dispatch('Login', this.loginForm).then((res) => {
            if (res.isSuccess == true) {
              this.loading = false
              this.$router.push({ path: this.redirect || '/' })
            } else {
              this.$message.error('用户名或密码错误')
            }
          }).catch(() => {
            this.loading = false
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    externalLogin(user) {
      this.$store.dispatch('MobileLogin', user).then(() => {
        this.loading = false
        this.$router.push({ path: this.redirect || '/' })
      }).catch(() => {
        this.loading = false
        // this.getCode()
      })
    }

  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
.login_title {
  //position: relative;
  //top: 50px;
  font-size: 60px;
  font-family: Source Han Sans CN;
  font-weight: bold;
  font-style: italic;
  color: #FFFFFF;
  padding: 20px 50px;
  margin-top: 170px;
  margin-bottom: 20px;
  background: linear-gradient(180deg, #F3FDFF 30%, #92B8FF 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}

.login {
  //display: flex;
  //height: 100vh;
  //width: 100vw;
  //min-width: 1000px;
  //overflow: hidden;
  //background-size: cover;

  display: flex;
  flex-direction: column;
  justify-content: start;
  align-items: center;
  height: 100vh;
  width: 100vw;
  background-image: url("../../assets/img/bg.png");
  background-size: cover;

  .login-form-wrap {
    width: 600px;
    height: 450px;
    padding: 25px;
    background-image: url("../../assets/img/denglu-kuang.png");
    background-size: 100% 100%;

    .login-form-content {
      display: flex;
      flex-direction: column;
      align-items: center;
    }

    .login-title-img {
      margin-top: 12px;
      margin-bottom: 80px;
    }

    .login-form {
      border-radius: 6px;
      width: 400px;


      .form-title {
        color: #ffffff;
        font-size: 14px;
        line-height: 4vh;
      }

      .el-input-group__prepend {
        padding: 0;
        border: none;
        background-color: transparent;
      }

      .el-input {
        height: 5vh;

        input {
          height: 5vh;
        }
      }

      .el-input__inner::placeholder {
        font-size: 16px;
        font-family: Source Han Sans CN;
        font-weight: 400;
        color: #9BA6D0;
      }

      .input-prefix-img {
        height: 46px;
        width: 50px;
        position: relative;
      }

      .input-prefix-img2 {
        height: 20px;
        width: 20px;
        position: absolute;
        left: 0;
        right: 0;
        top: 0;
        bottom: 0;
        margin: auto;
      }


      .el-input__inner {
        background-color: #002095;
        border: 1px solid #4068FF;
        color: #ffffff;
        font-size: 16px;
        font-family: Source Han Sans CN;
        font-weight: 400;
      }

      .el-input__inner:hover {
        border: 1px solid #0C75EC;
      }

      .el-checkbox__inner {
        border-radius: 22%;
        width: 15px;
        height: 15px;
      }

      .el-input--prefix .el-input__inner {
        padding-left: 50px;
      }

      input::-webkit-input-placeholder {
        color: #4E5566;
      }

      input::-moz-input-placeholder {
        color: #4E5566;
      }

      input::-ms-input-placeholder {
        color: #4E5566;
      }

      .code-wrap {
        display: flex;
        justify-content: space-between;
        align-items: center;

        .code-input {
          width: 43%;
        }

        .rImg {
          background: #2C385B;
          border: 1px solid #000000;
          border-radius: 4px;
          width: 35%;
          height: 4.5vh;
          cursor: pointer;
        }
      }

      .el-checkbox__inner {
        border: 1px solid #DCDFE6;
        background-color: rgba(0, 0, 0, 0);
      }

      .el-checkbox__inner:hover {
        border: 1px solid #1890ff;
        background-color: rgba(0, 0, 0, 0);
      }

      .el-checkbox__input.is-checked .el-checkbox__inner {
        background-color: #1890ff;
        border-color: #1890ff;
      }

      .el-checkbox__inner::after {
        -webkit-box-sizing: content-box;
        box-sizing: content-box;
        content: "";
        color: #ffffff;
        border-bottom: #ffffff solid 2px;
        border-right: #ffffff solid 2px;
        font-weight: bold;
      }

      .el-checkbox__label {
        color: #ffffff;
      }

      .el-checkbox__input.is-checked+.el-checkbox__label {
        color: #1890ff;
      }

      .app-wrap {
        display: flex;
        align-items: center;
        cursor: pointer;

        .app-img {
          width: 20px;
        }

        .app-text {
          margin-left: 5px;
          color: #fff;
          font-size: 18px;
        }
      }

      .login-btn {
        width: 100%;
        height: 5vh;
        background-size: 100% 100%;
        background-repeat: no-repeat;
        background: linear-gradient(0deg, #004EFF 0%, #00A8FF 100%);
        border-radius: 8px;

        font-size: 18px;
        font-family: Source Han Sans CN;
        font-weight: bold;
        color: #FFFFFF;
      }

      .login-btn:hover {}

      .login-btn:active {}
    }
  }

}


.el-message-box {
  .el-button--primary {
    color: #000000;
    background-color: #1890ff;
    border-color: #1890ff;
  }
}
</style>
